﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Windows;
using System.Xml.Linq;
using System.Xml.XPath;


namespace HWAlarmGroupCreator
{
    public class HWDisplayGraphic
    {
        private string _name;
        public string Name
        {
            get { return _name ?? (_name = Regex.Replace(Path, @".+\\(.+)\.htm", "$1")); }
        }

        public string Path { get; private set; }
        public List<HWDataConnection> Points { get; private set; }

        public HWDisplayGraphic(string path) 
        {
            Path = path;
            Points = new List<HWDataConnection>();

            XDocument xDocument = XDocument.Load(path.Replace(".htm", @"_files\DS_datasource1.dsd"));

            var pointParameter = xDocument.XPathSelectElements(@"//property[@name='PointRefPointName']")
                .Where(point =>
                    {
                        var parameter = point.XPathSelectElement(@"../property[@name='PointRefParamName']");
                        return point.Value != "" && parameter.Value != "" && parameter.Value != "NAME";
                    })
                .Select(point => new HWDataConnection(point.Value))
                .Distinct()
                .OrderBy(x=>x.PointName);

            Points = new List<HWDataConnection>(pointParameter);
        }

        public void CopyToClipboard(object sender, RoutedEventArgs e)
        {
            Clipboard.SetText(Points.Aggregate("", (current, point) => current + (point + Environment.NewLine)));
        }
    }
}
